<template>
  <nav class="site-navbar" :class="`site-navbar--${$store.state.navbarLayoutType}`">
    <div class="site-navbar__header">
      <h1 class="site-navbar__brand" @click="$router.push({ name: 'home' })">
        <a class="site-navbar__brand-lg" href="javascript:;">{{ $t('message.title') }}</a>
        <a class="site-navbar__brand-mini" href="javascript:;">IM</a>
      </h1>
    </div>
    <div class="site-navbar__body">
      <el-menu class="site-navbar__menu mr-auto" mode="horizontal">
        <el-menu-item class="site-navbar__switch" index="0" @click="$store.state.sidebarFold = !$store.state.sidebarFold">
          <icon-svg name="zhedie"></icon-svg>
        </el-menu-item>
      </el-menu>
      <el-menu class="site-navbar__menu" mode="horizontal">
        <el-menu-item index="1-1" class="hidden-xs-only">
          <!-- <a href="//www.renren.io/" target="_blank" >官方社区</a> -->
          <span href=""  @click="changeLangEvent(zh-CN)">中文</span>
        </el-menu-item>
        <el-menu-item index="1-2" class="hidden-xs-only">
          <!-- <a href="//www.renren.io/enterprise" target="_blank">产品授权</a> -->
          <span href="" @click="changeLangEvent('en-US')">English</span>
        </el-menu-item>
        <el-menu-item class="site-navbar__avatar" index="2">
          <el-dropdown :show-timeout="0" placement="bottom">
            <span class="el-dropdown-link">
              <img src="~@/assets/img/avatar.png" :alt="$store.state.user.name">
              <span>{{ $store.state.user.name }}</span>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="updatePasswordHandle()">{{ $t('message.updatepassword') }}</el-dropdown-item>
              <el-dropdown-item @click.native="logoutHandle()">{{ $t('message.logout') }}</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-menu-item>
      </el-menu>
    </div>
    <!-- 弹窗, 修改密码 -->
    <update-password v-if="updatePassowrdVisible" ref="updatePassowrd"></update-password>
  </nav>
</template>

<script>
  import UpdatePassword from './main-navbar-update-password'
  import { clearLoginInfo } from '@/utils'
  export default {
    data () {
      return {
        updatePassowrdVisible: false
      }
    },
    components: {
      UpdatePassword
    },
    methods: {
      /**
       * 切换语言
       */
      changeLangEvent (language) {
        // alert(this.$i18n.locale);
        if (language === 'en-US') {
          this.lang = 'en-US'
          this.$i18n.locale = this.lang
        } else {
          this.lang = 'zh-CN'
          this.$i18n.locale = this.lang
        }
      },
      // 修改密码
      updatePasswordHandle () {
        this.updatePassowrdVisible = true
        this.$nextTick(() => {
          this.$refs.updatePassowrd.init()
        })
      },
      // 退出
      logoutHandle () {
        this.$confirm(`确定进行[退出]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http.post(`${window.SITE_CONFIG['baseURL']}/sys/logout`).then(({data}) => {
            if (data && data.code === 0) {
              clearLoginInfo()
              this.$router.push({ name: 'login' })
            }
          })
        }).catch(() => {})
      }
    }
  }
</script>
